using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace AA_Defence
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D Cloud;
        Texture2D Ground;
        Texture2D Cannon;
        Texture2D Cannon_Barrel;
        Texture2D Reticle;

        Texture2D Bomber;

        SoundEffect Music;
        SoundEffect Explosion;


        Vector2 gunPosition = Vector2.Zero;
        Vector2 cloudPosition = Vector2.Zero;
        Vector2 cloudSpeed = new Vector2(0.2f, 0.2f);
        Vector2 groundPosition = new Vector2(0, 400);
        Vector2 cannonPosition = new Vector2(725, 345);
        Vector2 cannon_basePosition = new Vector2(670, 340);

        Vector2 bomberPosition = Vector2.Zero;
        Vector2 bomberSpeed = new Vector2(50.0f, 50.0f);

        Vector2 cloudpos1 = Vector2.Zero;
        Vector2 cloudpos2 = new Vector2(0.0f, 100);
        Vector2 cloudpos3 = new Vector2(0.0f, 150);
        Vector2 jetpos1 = Vector2.Zero;
        Vector2 jetpos2 = new Vector2(-100.0f,100);
        Vector2 jetpos3 = new Vector2(-200.0f,0);
        Vector2 SquarePosition;

        float cloudspeed1 = 0.2f;

        float jetspeed1 = 0.8F;
        float jetspeed0 = 0.0f;

        float rotation;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

        }


        protected override void Initialize()
        {
            this.IsMouseVisible = false;
            base.Initialize();

        }

        protected override void LoadContent()
        {
            
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Cannon = Content.Load<Texture2D>("cannon");
            SquarePosition = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
            Ground = Content.Load<Texture2D>("ground");
            Cloud = Content.Load<Texture2D>("cloud");
            Bomber = Content.Load<Texture2D>("bomber");
            Cannon_Barrel = Content.Load<Texture2D>("cannon_barrel");
            Reticle = Content.Load<Texture2D>("reticle");
            Music = Content.Load<SoundEffect>("AA_Defence_theme");
            Explosion = Content.Load<SoundEffect>("88_explosion");

            Music.Play();
            
        }


        protected override void UnloadContent()
        {

        }


        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            cloudpos1.X += cloudspeed1;
            cloudpos2.X += (cloudspeed1*2);
            cloudpos3.X += (cloudspeed1*3);

            if (cloudpos1.X > Window.ClientBounds.Width)
                cloudpos1.X = (cloudspeed1-100.0f);
            if (cloudpos2.X > Window.ClientBounds.Width)
                cloudpos2.X = (cloudspeed1 - 100.0f);
            if (cloudpos3.X > Window.ClientBounds.Width)
                cloudpos3.X = (cloudspeed1 - 100.0f);

            jetpos1.X += jetspeed1;
            jetpos2.X += jetspeed1;
            jetpos3.X += jetspeed1;

            if (jetpos1.X > Window.ClientBounds.Width)
            {
                jetpos1.X = (jetspeed0-100.0f);
            }
            if (jetpos2.X > Window.ClientBounds.Width)
            {
                jetpos2.X = (jetspeed0 - 100.0f);
            }
            if (jetpos3.X > Window.ClientBounds.Width)
            {
                jetpos3.X = (jetspeed0 - 100.0f);
            }
            // Store the mouse state
            MouseState mouse = Mouse.GetState();

            // Calculate the distance from the square to the mouse's X and Y position
            float XDistance = SquarePosition.X - mouse.X;
            float YDistance = SquarePosition.Y - mouse.Y;

            // Calculate the required rotation by doing a two-variable arc-tan
            //rotation = (float)Math.Atan2(YDistance, XDistance);
            rotation = (float)Math.Atan2(cannonPosition.Y - mouse.Y, cannonPosition.X - mouse.X) - (float)(Math.PI/7);
            //rotation = 0;

            gunPosition.X = Mouse.GetState().X;
            gunPosition.Y = Mouse.GetState().Y;


            
            // Gun fire when mouseclicked

            MouseState current_mouse = Mouse.GetState();
            if (current_mouse.LeftButton == ButtonState.Pressed)
            {

                Explosion.Play();
                //MediaPlayer.Play(88_explosion);
            }


            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {

            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            spriteBatch.Draw(Cloud, cloudpos1, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(Cloud, cloudpos2, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(Cloud, cloudpos3, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(Ground, groundPosition, Color.White);
            spriteBatch.Draw(Cannon, cannon_basePosition, Color.White);
            spriteBatch.Draw(Bomber, jetpos1, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(Bomber, jetpos2, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(Bomber, jetpos3, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(Cannon_Barrel, cannonPosition, null, Color.White, rotation, new Vector2(Cannon.Width / 2, Cannon.Height / 2), 1.0f, SpriteEffects.None, 0f);
            Primitives2D.DrawCircle(spriteBatch, cannonPosition, 4, 20, Color.White);
            spriteBatch.Draw(Reticle, new Rectangle((int)gunPosition.X, (int)gunPosition.Y, 24, 24),Color.White);
            spriteBatch.End();
            
            base.Draw(gameTime);
        }
    }
}
